package com.fmboy.MXH.nav.config;

import com.fmboy.MXH.nav.interceptor.JwtUserInterceptor;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${file.staticAccessPath}") // 静态资源访问路径
    private String staticAccessPath;
    @Value("${file.upload-dir}") // 文件上传路径
    private String uploadFolder;
    @Resource
    private JwtUserInterceptor jwtUserInterceptor;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 添加 JSON 消息转换器
        converters.add(new MappingJackson2HttpMessageConverter());

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtUserInterceptor)
                .excludePathPatterns("/mxh-nav/login")  // 登录接口不受保护
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/v3/**", "/webjars/**", "/doc.html", "/swagger-ui.html/**")  // 排除Swagger接口
                .excludePathPatterns("/")  // 主页排除
                .excludePathPatterns("/mxh-nav/email")  // 邮箱验证码接口排除
                .excludePathPatterns("/mxh-nav/register")  // 注册接口排除
                .excludePathPatterns("/mxh-nav/password/reset");  // 重置密码接口排除
    }

    // 配置静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(staticAccessPath)
                .addResourceLocations("file:" + uploadFolder);
    }
}